package com.cd.javacxy.common.utils;

import com.cd.javacxy.common.model.vo.CxyCategoryVo;
import com.cd.javacxy.module.web.service.CxyCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 缓存分类
 *
 * @author cookie
 */
@Component
public class CategoryCache {

    private CxyCategoryService cxyCategoryService;

    @Autowired
    public void setCxyCategoryService(CxyCategoryService cxyCategoryService) {
        this.cxyCategoryService = cxyCategoryService;
    }

    private static Map<String, List<CxyCategoryVo>> categoryCache;
    private static Map<String, CxyCategoryVo> singleCategoryCache;

    static {
        categoryCache = new ConcurrentHashMap<>();
        singleCategoryCache = new ConcurrentHashMap<>();
        categoryCache.put(Constants.CACHE_KEY_BBS_CATEGORY, new ArrayList<>());
        categoryCache.put(Constants.CACHE_KEY_KNOWLEDGE_CATEGORY, new ArrayList<>());
        categoryCache.put(Constants.CACHE_KEY_ASK_CATEGORY, new ArrayList<>());
        categoryCache.put(Constants.CACHE_KEY_EXAM_CATEGORY, new ArrayList<>());
    }

    public void filterChildren(CxyCategoryVo c, String show) {
        List<CxyCategoryVo> filterChildren = new ArrayList<>();
        List<CxyCategoryVo> children = c.getChildren();
        for (CxyCategoryVo category : children) {
            if (show.equals(Constants.CACHE_KEY_BBS_CATEGORY) && Constants.Y.equals(category.getShowInBbs())) {
                filterChildren.add(category);
            }
            if (show.equals(Constants.CACHE_KEY_ASK_CATEGORY) && Constants.Y.equals(category.getShowInQuestion())) {
                filterChildren.add(category);
            }
            if (show.equals(Constants.CACHE_KEY_KNOWLEDGE_CATEGORY) && Constants.Y.equals(category.getShowInKnowledge())) {
                filterChildren.add(category);
            }
            if (show.equals(Constants.CACHE_KEY_EXAM_CATEGORY) && Constants.Y.equals(category.getShowInExam())) {
                filterChildren.add(category);
            }
            singleCategoryCache.put(Constants.CACHE_KEY_CATEGORY + category.getCategoryId(), category);
        }
        c.setChildren(filterChildren);
    }

    public void refreshCategoryCache() {
        List<CxyCategoryVo> list = this.cxyCategoryService.findCategoryList(null);
        for (CxyCategoryVo category : list) {
            if (Constants.Y.equals(category.getShowInBbs())) {
                categoryCache.get(Constants.CACHE_KEY_BBS_CATEGORY).add(category);
                filterChildren(category, Constants.CACHE_KEY_BBS_CATEGORY);
            }
            if (Constants.Y.equals(category.getShowInExam())) {
                categoryCache.get(Constants.CACHE_KEY_EXAM_CATEGORY).add(category);
                filterChildren(category, Constants.CACHE_KEY_EXAM_CATEGORY);
            }
            if (Constants.Y.equals(category.getShowInKnowledge())) {
                categoryCache.get(Constants.CACHE_KEY_KNOWLEDGE_CATEGORY).add(category);
                filterChildren(category, Constants.CACHE_KEY_KNOWLEDGE_CATEGORY);
            }
            if (Constants.Y.equals(category.getShowInQuestion())) {
                categoryCache.get(Constants.CACHE_KEY_ASK_CATEGORY).add(category);
                filterChildren(category, Constants.CACHE_KEY_ASK_CATEGORY);
            }
            singleCategoryCache.put(Constants.CACHE_KEY_CATEGORY + category.getCategoryId(), category);
        }
    }

    public List<CxyCategoryVo> getBbsCategories() {
        return categoryCache.get(Constants.CACHE_KEY_BBS_CATEGORY);
    }

    public List<CxyCategoryVo> getKnowledgeCategories() {
        return categoryCache.get(Constants.CACHE_KEY_KNOWLEDGE_CATEGORY);
    }

    public static CxyCategoryVo getCategoryById(Integer categoryId) {
        return singleCategoryCache.get(Constants.CACHE_KEY_CATEGORY + categoryId);
    }
}
